home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Devices and Hardware / Serial / SerialPortSample / SerialPortSample.c < prev   
Encoding:
C/C++ Source or Header  |  2000-10-10  |  16.7 KB  |  366 lines  |  [????/????]

  1. /*
  2.     File:        SerialPortSample.c
  3.     
  4.     Description:    This sample demonstrates how to use IOKitLib to find all serial ports on the system. 
  5.                         It also shows how to open, write to, read from, and close a serial port.
  6.                 
  7.     Author:        gc
  8.  
  9.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  10.     
  11.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  12.                         ("Apple") in consideration of your agreement to the following terms, and your
  13.                         use, installation, modification or redistribution of this Apple software
  14.                         constitutes acceptance of these terms.  If you do not agree with these terms,
  15.                         please do not use, install, modify or redistribute this Apple software.
  16.  
  17.                         In consideration of your agreement to abide by the following terms, and subject
  18.                         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  19.                         copyrights in this original Apple software (the "Apple Software"), to use,
  20.                         reproduce, modify and redistribute the Apple Software, with or without
  21.                         modifications, in source and/or binary forms; provided that if you redistribute
  22.                         the Apple Software in its entirety and without modifications, you must retain
  23.                         this notice and the following text and disclaimers in all such redistributions of
  24.                         the Apple Software.  Neither the name, trademarks, service marks or logos of
  25.                         Apple Computer, Inc. may be used to endorse or promote products derived from the
  26.                         Apple Software without specific prior written permission from Apple.  Except as
  27.                         expressly stated in this notice, no other rights or licenses, express or implied,
  28.                         are granted by Apple herein, including but not limited to any patent rights that
  29.                         may be infringed by your derivative works or by other works in which the Apple
  30.                         Software may be incorporated.
  31.  
  32.                         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  33.                         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  34.                         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35.                         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  36.                         COMBINATION WITH YOUR PRODUCTS.
  37.  
  38.                         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  39.                         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  40.                         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41.                         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  42.                         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  43.                         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  44.                         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.                 
  46.     Change History (most recent first):
  47.         
  48.             <2>         08/22/00    gc        Incorporated changes from code review.
  49.             <1>         08/03/00    gc        New sample.
  50.         
  51. */
  52.  
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. #include <fcntl.h>
  57. #include <errno.h>
  58. #include <paths.h>
  59. #include <termios.h>
  60. #include <sys/param.h>
  61. #include <IOKit/IOKitLib.h>
  62. #include <IOKit/IOBSD.h>
  63.  
  64. #define kIOPortDeviceNubClass    "IOPortDeviceNub"
  65. #define kDeviceTypeKey        "device_type"
  66. #define kSerialValue        "serial"
  67. #define kPortNameKey        "Port Name"
  68. #define kModemValue        "modem"
  69. #define kATCommandString    "AT\r"
  70. #define kOKResponseString    "OK"
  71.  
  72. enum {
  73.     kNumRetries = 3
  74. };
  75.  
  76. static kern_return_t FindSerialPorts( io_iterator_t *matchingServices );
  77. static kern_return_t FindModem( io_iterator_t serialPortIterator, char *bsdPath, CFIndex maxPathSize );
  78. static int OpenSerialPort( const char *bsdPath );
  79. static Boolean InitializeModem( int fileDescriptor );
  80. static void CloseSerialPort( int fileDescriptor );
  81.  
  82. // Returns an iterator across all serial ports. Caller is responsible for releasing the iterator
  83. // when iteration is complete.
  84. kern_return_t FindSerialPorts( io_iterator_t *matchingServices )
  85. {
  86.     kern_return_t        kernResult; 
  87.     mach_port_t            masterPort;
  88.     CFMutableDictionaryRef    classesToMatch;
  89.  
  90. /*! @function IOMasterPort
  91.     @abstract Returns the mach port used to initiate communication with IOKit.
  92.     @discussion Functions that don't specify an existing object require the IOKit master port to be passed. This function obtains that port.
  93.     @param size The task's bootstrap port, pass bootstrap_port from mach/mach_init.h.
  94.     @param masterPort The master port is returned.
  95.     @result A kern_return_t error code. */
  96.  
  97.     kernResult = IOMasterPort( bootstrap_port, &masterPort );
  98.     if ( KERN_SUCCESS != kernResult )
  99.         printf( "IOMasterPort returned %d\n", kernResult );
  100.         
  101. /*! @function IOServiceMatching
  102.     @abstract Create a matching dictionary that specifies an IOService class match.
  103.     @discussion A very common matching criteria for IOService is based on its class. IOServiceMatching will create a matching dictionary that specifies any IOService of a class, or its subclasses. The class is specified by C-string name.
  104.     @param name The class name, as a const C-string. Class matching is successful on IOService's of this class or any subclass.
  105.     @result The matching dictionary created, is returned on success, or zero on failure. The dictionary is commonly passed to IOServiceGetMatchingServices or IOServiceAddNotification which will consume a reference, otherwise it should be released with CFRelease by the caller. */
  106.  
  107.     // Serial devices are instances of class kIOPortDeviceNubClass
  108.     classesToMatch = IOServiceMatching( kIOPortDeviceNubClass ); 
  109.     if ( classesToMatch == NULL )
  110.         printf( "IOServiceMatching returned a NULL dictionary.\n" );
  111.     else {
  112. /*!
  113.     @function CFDictionarySetValue
  114.     Sets the value of the key in the dictionary.
  115.     @param theDict The dictionary to which the value is to be set. If this
  116.         parameter is not a valid mutable CFDictionary, the behavior is
  117.         undefined. If the dictionary is a fixed-capacity dictionary and
  118.         it is full before this operation, and the key does not exist in
  119.         the dictionary, the behavior is undefined.
  120.     @param key The key of the value to set into the dictionary. If a key 
  121.         which matches this key is already present in the dictionary, only
  122.         the value is changed ("add if absent, replace if present"). If
  123.         no key matches the given key, the key-value pair is added to the
  124.         dictionary. If added, the key is retained by the dictionary,
  125.         using the retain callback provided
  126.         when the dictionary was created. If the key is not of the sort
  127.         expected by the key retain callback, the behavior is undefined.
  128.     @param value The value to add to or replace into the dictionary. The value
  129.         is retained by the dictionary using the retain callback provided
  130.         when the dictionary was created, and the previous value if any is
  131.         released. If the value is not of the sort expected by the
  132.         retain or release callbacks, the behavior is undefined.
  133. */
  134.  
  135.         CFDictionarySetValue( classesToMatch, CFSTR( kDeviceTypeKey ), CFSTR( kSerialValue ) ); 
  136.         // Each serial device object has a property with key kDeviceTypeKey and value kSerialValue
  137.         // so add this property to the CFDictionary we're matching on. 
  138.     }
  139.     
  140.     /*! @function IOServiceGetMatchingServices
  141.         @abstract Look up registered IOService objects that match a matching dictionary.
  142.         @discussion This is the preferred method of finding IOService objects currently registered by IOKit. IOServiceAddNotification can also supply this information and install a notification of new IOServices. The matching information used in the matching dictionary may vary depending on the class of service being looked up.
  143.         @param masterPort The master port obtained from IOMasterPort().
  144.         @param matching A CF dictionary containing matching information, of which one reference is consumed by this function. IOKitLib can contruct matching dictionaries for common criteria with helper functions such as IOServiceMatching, IOOpenFirmwarePathMatching.
  145.         @param existing An iterator handle is returned on success, and should be released by the caller when the iteration is finished.
  146.         @result A kern_return_t error code. */
  147.  
  148.     kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, matchingServices );    
  149.     if ( KERN_SUCCESS != kernResult )
  150.         printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
  151.         
  152.     return kernResult;
  153. }
  154.     
  155. // Given an iterator across a set of serial ports, find the first modem and return the BSD path to the
  156. // modem device. If no modems are found the path name is set to an empty string.
  157. kern_return_t FindModem( io_iterator_t serialPortIterator, char *bsdPath, CFIndex maxPathSize )
  158. {
  159.     io_object_t        nextService;
  160.     kern_return_t    kernResult = KERN_FAILURE;
  161.     Boolean        modemFound = false;
  162.     
  163.     /*! @function IOIteratorNext
  164.     @abstract Returns the next object in an iteration.
  165.     @discussion This function returns the next object in an iteration, or zero if no more remain or the iterator is invalid.
  166.     @param iterator An IOKit iterator handle.
  167.     @result If the iterator handle is valid, the next element in the iteration is returned, otherwise zero is returned. */
  168.  
  169.     while ( nextService = IOIteratorNext( serialPortIterator ) )
  170.     {
  171.         CFTypeRef    portNameAsCFString;
  172.         CFTypeRef    bsdPathAsCFString;
  173.         char        portNameAsCString[ MAXPATHLEN ];
  174.         
  175. /*! @function IORegistryEntryCreateCFProperty
  176.     @abstract Create a CF representation of a registry entry's property.
  177.     @discussion This function creates an instantaneous snapshot of a registry entry property, creating a CF container analogue in the caller's task. Not every object available in the kernel is represented as a CF container; currently OSDictionary, OSArray, OSSet, OSSymbol, OSString, OSData, OSNumber, OSBoolean are created as their CF counterparts. 
  178.     @param entry The registry entry handle whose property to copy.
  179.     @param key A CFString specifying the property name.
  180.     @param allocator The CF allocator to use when creating the CF container.
  181.     @param options No options are currently defined.
  182.     @result A CF container is created and returned the caller on success. The caller should release with CFRelease. */
  183.  
  184.         portNameAsCFString = IORegistryEntryCreateCFProperty( nextService, 
  185.                                                               CFSTR( kPortNameKey ), 
  186.                                                               kCFAllocatorDefault, 
  187.                                                               0 );
  188.  
  189.         *portNameAsCString = '\0';
  190.         if ( portNameAsCFString )
  191.         {
  192.             if ( CFStringGetCString( portNameAsCFString, 
  193.                                      portNameAsCString, 
  194.                                      sizeof( portNameAsCString ), 
  195.                                      kCFStringEncodingASCII ) )
  196.             {
  197.                 printf( "Port name: %s, ", portNameAsCString );
  198.                 kernResult = KERN_SUCCESS;
  199.             }
  200.  
  201.             CFRelease( portNameAsCFString );
  202.         }
  203.  
  204.         bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextService, 
  205.                                                              CFSTR( kIOBSDName ), 
  206.                                                              kCFAllocatorDefault, 
  207.                                                              0 );
  208.  
  209.         *bsdPath = '\0';
  210.         if ( bsdPathAsCFString )
  211.         {
  212.             size_t devPathLength = strlen( _PATH_DEV );
  213.             
  214.             strcpy( bsdPath, _PATH_DEV );
  215.             
  216.             if ( CFStringGetCString( bsdPathAsCFString,
  217.                                      bsdPath + devPathLength,
  218.                                      maxPathSize - devPathLength, 
  219.                                      kCFStringEncodingASCII ) )
  220.             {
  221.                 printf( "BSD path: %s", bsdPath );
  222.             }
  223.             
  224.             CFRelease( bsdPathAsCFString );
  225.         }
  226.             
  227.         // See if this port has "modem" in its name
  228.         if ( strstr( portNameAsCString, kModemValue ) )
  229.         {
  230.             modemFound = true;
  231.             printf( " <---modem found!\n" );
  232.             break;
  233.         }
  234.         
  235.         printf( "\n" );
  236.  
  237. /*! @function IOObjectRelease
  238.     @abstract Releases an object handle previously returned by IOKitLib.
  239.     @discussion All objects returned by IOKitLib should be released with this function when access to them is no longer needed. Using the object after it has been released may or may not return an error, depending on how many references the task has to the same object in the kernel.
  240.     @param object The IOKit object to release.
  241.     @result A kern_return_t error code. */
  242.     
  243.         kernResult = IOObjectRelease( nextService );
  244.     }
  245.  
  246.     if ( !modemFound )
  247.         *bsdPath = '\0';
  248.         
  249.     return kernResult;
  250. }
  251.  
  252. // Given the path to a serial device, open the device and configure it.
  253. // Return the file descriptor associated with the device.
  254. int OpenSerialPort( const char *bsdPath )
  255. {
  256.     int         fileDescriptor;
  257.     struct termios    options;
  258.     
  259.     fileDescriptor = open( bsdPath, O_RDWR | O_NOCTTY | O_NDELAY );
  260.     
  261.     if ( fileDescriptor == -1 )
  262.         printf( "Error %d opening serial port %s.\n", errno, bsdPath );
  263.     else {
  264.         fcntl( fileDescriptor, F_SETFL, 0 );
  265.     
  266.         // Get the current options
  267.         tcgetattr( fileDescriptor, &options );
  268.         
  269.         // Set raw input, one second timeout
  270.         // These options are documented in the man page for termios
  271.         // (in Terminal enter: man termios)
  272.         options.c_cflag |= ( CLOCAL | CREAD );
  273.         options.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );
  274.         options.c_oflag &= ~OPOST;
  275.         options.c_cc[ VMIN ] = 0;
  276.         options.c_cc[ VTIME ] = 10;
  277.         
  278.         // Set the options
  279.         tcsetattr( fileDescriptor, TCSANOW, &options );
  280.     }
  281.     
  282.     return fileDescriptor;
  283. }
  284.  
  285. // Given the file descriptor for a modem device, attempt to initialize the modem by sending it
  286. // a standard AT command and reading the response. If successful the response is "OK".
  287. // Return true if successful, otherwise false.
  288. Boolean InitializeModem( int fileDescriptor )
  289. {
  290.     char    buffer[ 255 ];    // Input buffer
  291.     char    *bufPtr;    // Current char in buffer
  292.     ssize_t    numBytes;    // Number of bytes read or written
  293.     int        tries;        // Number of tries so far
  294.     
  295.     for ( tries = 0; tries < kNumRetries; tries++ )
  296.     {
  297.         // Send an AT command to the modem
  298.         numBytes = write( fileDescriptor, kATCommandString, strlen( kATCommandString ) );
  299.         if ( numBytes < strlen( kATCommandString ) ) 
  300.             continue;
  301.             
  302.         // Read characters into our buffer until we get a CR or LF
  303.         bufPtr = buffer;
  304.         while ( ( numBytes = read( fileDescriptor, bufPtr, buffer + sizeof( buffer ) - bufPtr - 1 ) ) > 0 )
  305.         {
  306.             bufPtr += numBytes;
  307.             if ( *( bufPtr - 1 ) == '\n' || *( bufPtr - 1 ) == '\r' )
  308.                 break;
  309.         }
  310.         
  311.         // NUL terminate the string and see if we got an OK response
  312.         *bufPtr = '\0';
  313.         
  314.         if ( strncmp( buffer, kOKResponseString, strlen( kOKResponseString) ) == 0 )
  315.             return false;
  316.     }
  317.     
  318.     return true;
  319. }
  320.  
  321. // Given the file descriptor for a serial device, close that device.
  322. void CloseSerialPort( int fileDescriptor )
  323. {
  324.     close( fileDescriptor );
  325. }
  326.  
  327. int main( int argc, char *argv[] )
  328. {
  329.     kern_return_t    kernResult; // on PowerPC this is an int (4 bytes)
  330. /*
  331.  *    error number layout as follows (see mach/error.h):
  332.  *
  333.  *    hi                        lo
  334.  *    | system(6) | subsystem(12) | code(14) |
  335.  */
  336.  
  337.     io_iterator_t    serialPortIterator;
  338.     char        bsdPath[ MAXPATHLEN ];
  339.  
  340.     kernResult = FindSerialPorts( &serialPortIterator );
  341.     
  342.     kernResult = FindModem( serialPortIterator, bsdPath, sizeof( bsdPath ) );
  343.     
  344.     // Now open the modem port we found, initialize the modem, and close the port
  345.     if ( bsdPath[ 0 ] != '\0' )
  346.     {
  347.         int fileDescriptor;
  348.         
  349.         fileDescriptor = OpenSerialPort( bsdPath );
  350.         
  351.         if ( InitializeModem( fileDescriptor ) )
  352.             printf( "Modem initialized.\n" );
  353.         else
  354.             printf( "Could not initialize modem.\n" );
  355.             
  356.         CloseSerialPort( fileDescriptor );
  357.         printf( "Modem port closed.\n" );
  358.     }
  359.     else
  360.         printf( "No modem port found.\n" );
  361.         
  362.     // Release the iterator.
  363.     IOObjectRelease( serialPortIterator );
  364.  
  365.     return 0;
  366. }